const commodityModel = require("../model/commodityModel");
const shopDoorModel = require("../model/shopDoorModel")
//查询所有门店
module.exports.find = async function (searchObj, page) {
    // 分页查询
    let { current, size } = page;
    // 数据总条数
    let total = await commodityModel.countDocuments(searchObj);
    // 获取总页数: 数据总数 / 每页显示条数（注意要向上取整）
    let totalPage = Math.ceil(total / size);
    let rows = await commodityModel.find(searchObj)
        .populate({ path: 'adminId' })
        .populate({ path: 'shopDoorId', middle: shopDoorModel })
        .limit(parseInt(size))
        .skip((current - 1) * size);
    return {
        current, size, total, totalPage, rows
    }
}
//根据id查
module.exports.findById = async function (id) {
    return await commodityModel.findById(id)
}
//增加
module.exports.insert = async function (commodity) {
    return await commodityModel.create(commodity)
};
//删除
module.exports.delete = async function (id) {
    return await commodityModel.deleteOne({ _id: id })
}

//修改
module.exports.update = async function (id, commodity) {
    return await commodityModel.updateOne({ _id: id }, commodity)
}